﻿# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.12)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set (CMAKE_LEGACY_CYGWIN_WIN32 0)

if (NOT DEFINED CMAKE_CXX_STANDARD)
    set(CMAKE_CXX_STANDARD 17)
endif()

set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.0)
    # Enable MACOSX_RPATH by default
    cmake_policy (SET CMP0042 NEW)
endif()

if (NOT DEFINED CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_NO_WARNINGS)
    set (CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_NO_WARNINGS ON)
endif()

project (SimpleKafka1C)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR})

find_package(Boost 1.38 REQUIRED COMPONENTS filesystem iostreams program_options regex system json)

find_package(Snappy)
if (SNAPPY_FOUND)
    set(SNAPPY_PKG libsnappy)
    add_definitions(-DSNAPPY_CODEC_AVAILABLE)
    message("Enabled snappy codec")
else (SNAPPY_FOUND)
    set(SNAPPY_PKG "")
    set(SNAPPY_LIBRARIES "")
    set(SNAPPY_INCLUDE_DIR "")
    message("Disabled snappy codec. libsnappy not found.")
endif (SNAPPY_FOUND)

find_package(RdKafka CONFIG REQUIRED)

add_definitions (${Boost_LIB_DIAGNOSTIC_DEFINITIONS})
add_definitions("-DLIBRDKAFKA_STATICLIB")

if (CMAKE_COMPILER_IS_GNUCXX)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -flto -s -O3 -DNDEBUG")
endif ()

set(SOURCE_FILES
    src/stdafx.h
    src/exports.cpp
    src/md5.cpp
    src/md5.h
    src/Component.cpp
    src/Component.h
    src/SimpleKafka1C.cpp
    src/SimpleKafka1C.h)

add_library(SimpleKafka1C SHARED ${SOURCE_FILES})

if (WIN32)
	find_package(unofficial-avro-cpp CONFIG REQUIRED)
	target_include_directories(SimpleKafka1C PRIVATE include ${SOURCES}/include 
		${Boost_INCLUDE_DIRS} 
		${RdKafka_INCLUDE_DIRS} 
		${unofficial-avro-cpp_INCLUDE_DIRS})
	target_link_libraries(SimpleKafka1C ${Boost_LIBRARIES}
		${SNAPPY_LIBRARIES} 
		RdKafka::rdkafka RdKafka::rdkafka++
		unofficial::avro-cpp::avrocpp)
else()
	target_include_directories(SimpleKafka1C PRIVATE include ${SOURCES}/include 
		${Boost_INCLUDE_DIRS} 
		${RdKafka_INCLUDE_DIRS} 
		/home/source/avro-src-1.11.3/lang/c++/api)
	target_link_libraries(SimpleKafka1C ${Boost_LIBRARIES}
		${SNAPPY_LIBRARIES} 
		/home/source/avro-src-1.11.3/lang/c++/libavrocpp_s.a
		/home/source/vcpkg/installed/x64-linux/lib/libboost_iostreams.a
		/home/source/vcpkg/installed/x64-linux/lib/libsnappy.a
		/home/source/vcpkg/installed/x64-linux/lib/libz.a
		RdKafka::rdkafka RdKafka::rdkafka++)
endif ()

if (WIN32 AND NOT MSVC)
    message(FATAL_ERROR "Must be compiled with MSVC on Windows")
endif ()

if (WIN32)
    add_definitions(-D_CRT_SECURE_NO_WARNINGS)
    target_compile_definitions(SimpleKafka1C PRIVATE
            _WINDOWS
            _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING)
    target_compile_options(SimpleKafka1C PRIVATE /utf-8)
else ()
	if (NOT CMAKE_BUILD_TYPE)
		set (CMAKE_BUILD_TYPE Release CACHE STRING
		  "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
		  FORCE)
	        message("CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}")
	endif (NOT CMAKE_BUILD_TYPE)
endif ()
